package com.ruoyi.common.utils.db;

import com.ruoyi.common.config.DatabaseConfig;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.enums.DatabaseType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: com.ruoyi.common.utils.db-> DatabaseUtils
 * @description: 数据库工具类
 * @author: andy  523333809@qq.com
 * @createDate: 2022/8/10 9:47
 * @version: 1.0
 */
public class DatabaseUtils {

    /**
     * 通过配置文件读取数据库配置
     * 返回配置的多个数据源的相关参数
     * map  -->
     * 	dataSourceType  MASTER,SLAVE
     * 	databaseType	PostgreSQL,Oracle,MySQL
     * 	driverClassName  org.postgresql.Driver ,oracle.jdbc.driver.OracleDriver,com.mysql.cj.jdbc.Driver
     *
     * @return
     */
    public static List<Map<String,Object>> getDatabaseConfigList(){
        List<Map<String,Object>> databaseConfigList = new ArrayList<>();
        Map<String,Object> druidMap = DatabaseConfig.getDruid();
        DataSourceType[] datasourceTypes = DataSourceType.values();
        for(int i = 0;i<datasourceTypes.length;i++){
            Map<String,Object> dbConfigmap = new HashMap<>();
            String dataSourceType = String.valueOf(datasourceTypes[i]).toLowerCase();
            dbConfigmap.put("dataSourceType",dataSourceType.toUpperCase());
            Map<String,Object> tempDbMap = (Map)druidMap.get(dataSourceType);
            String driverClassName = tempDbMap.get("driverClassName").toString();
            if(null==tempDbMap.get("enabled")){
                dbConfigmap.put("driverClassName",driverClassName);
                for(DatabaseType dt: DatabaseType.values()){
                    if(dt.getDriverClass().equals(driverClassName)){
                        dbConfigmap.put("databaseType",dt.toString());
                        break;
                    }
                }
            }else if("true".equals(tempDbMap.get("enabled").toString().toLowerCase())){
                dbConfigmap.put("driverClassName",tempDbMap.get("driverClassName").toString());
                for(DatabaseType dt: DatabaseType.values()){
                    if(dt.getDriverClass().equals(driverClassName)){
                        dbConfigmap.put("databaseType",dt.toString());
                        break;
                    }
                }
            }
            databaseConfigList.add(dbConfigmap);
        }
        return databaseConfigList;
    }

    /**
     * 根据传入的dataSourceType 查询出数据库类型
     * @param dataSourceType
     * @return
     */
    public static String getDatabaseType(String dataSourceType){
        String databaseType = "";
        Map<String,Object> druidMap = DatabaseConfig.getDruid();
        Map<String,Object> tempDbMap = (Map)druidMap.get(dataSourceType.toLowerCase());
        String driverClassName = tempDbMap.get("driverClassName").toString();
        for(DatabaseType dt: DatabaseType.values()){
            if(dt.getDriverClass().equals(driverClassName)){
                databaseType = dt.toString();
                break;
            }
        }
        return databaseType;
    }



    public static void main(String[] args){
        System.out.println("===="+DatabaseType.valueOf("MySQL"));
    }

}
